package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        //1. 设置分页参数 完成页数和每页显示几个数据的设置
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());

        //2. 执行查询
        List<Student> studentList = studentMapper.list(studentQueryParam);


        //3. 封装分页结果
        Page<Student> p = (Page<Student>) studentList;
        //为什么要强转为Page<Emp>呢？因为Page<Emp>中有很多分页相关的信息，如总记录数等。
        return new PageResult(p.getTotal(), p.getResult());
        //p.getResult()获取当前页的结果集是一个List泛型是Student

    }
    //删除学生信息
    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    //添加学生信息
    @Override
    public void save(Student student) {
        studentMapper.save(student);
    }

    //根据ID查询学生信息查询回显
    @Override
    public Student findById(Integer id) {
        return studentMapper.findById(id);
    }

    //修改学生信息
    @Override
    public void update(Student student) {
        //1.补全基本信息
        student.setUpdateTime(LocalDateTime.now());
        //2.调用dao层保存数据
        studentMapper.update(student);
    }

    //违纪处理 复用
    //违纪处理一次，需要将违纪次数+1，违纪扣分+前端输入的分数
    @Override
    public void violation(Integer id, Integer score) {
        //1.查询学生信息 根据前端传递的id查询具体修改那位学生的信息
        Student student = studentMapper.findById(id);
        //2.更新学生信息
        student.setViolationCount((short) (student.getViolationCount() + 1));
        student.setViolationScore((short) (student.getViolationScore() + score));
        studentMapper.update(student);
    }



}
